package com.jww.cloud.auth.service.impl;

import com.jww.base.am.model.dos.SysUserDO;
import com.jww.cloud.service.user.client.RoleProviderClient;
import com.jww.cloud.service.user.client.UserProviderClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * 获取用户信息实现类
 *
 * @author wanyong
 * @date 2018/9/24 15:11
 */
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserProviderClient userProviderClient;

    @Autowired
    private RoleProviderClient roleProviderClient;

    @Override
    public UserDetailsImpl loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUserDO sysUserDO = userProviderClient.queryByUsername(username);
        if (ObjectUtils.isEmpty(sysUserDO)) {
            throw new UsernameNotFoundException("用户不存在");
        }
        // 获取用户对应角色
        sysUserDO.setRoleList(roleProviderClient.listByUserId(sysUserDO.getId()));
        return new UserDetailsImpl(sysUserDO);
    }
}
